#!/bin/bash

shopt -s nullglob

working_dir=$(pwd)
module_name=$(basename ${working_dir})

case ${working_dir} in
    /usr/oem-dev/*/*)
        oem_name=$(basename $(dirname ${working_dir}))
        module_name="${oem_name}-${module_name}"
    ;;
esac



function get_sandboxes
{
    unset sandboxes[*]
    for sandbox in ${USER}-*
    do
        if [ -d ${sandbox} ]
        then
            sandboxes[${#sandboxes[*]}]=${sandbox}
        fi
    done
}

function remove_sandbox
{
    sandbox=${1}

    if [ -d ${sandbox} ]
    then
        base_dir=${sandbox##${USER}-}
        (cd ${sandbox}/src && cvs update)
        
        cvs-retire-version ${module_name} ${sandbox} current #${base_dir}
    else
        echo "Can't find sandbox named '${sandbox}'. Check spelling and working directory."
    fi
    
}

if [ $# -eq 1 ]
then
    remove_sandbox ${1%/}
else

    get_sandboxes
    
    if [ -z ${sandboxes[*]} ]
    then
        echo no sandbox here
        exit
    fi

    echo select a sandbox, or hit \"q\" to quit
    select sandbox in ${sandboxes[*]}
    do
        if [ -z ${sandbox} ]
        then
            echo quitting
            exit 0
        fi
        
        remove_sandbox ${sandbox}
        break;
    done
fi
